import com.limetalks.data.Stream;
import com.limetalks.events.LimeEvent;
import com.limetalks.logic.LimeTalksSphere;

import flash.events.NetStatusEvent;
import flash.external.ExternalInterface;
import flash.net.NetConnection;

import mx.controls.Alert;
import mx.core.Application;
import mx.events.FlexEvent;

private var limeTalks:LimeTalksSphere;
private var connection:NetConnection;

private var streams:Array;
private var category:int;

private function update(categoryId:int):void {
	category = categoryId;
	filterByCategory(streams);
}

private function filterByCategory(streamArray:Array):void {
	
	if(category > 0) {
		var streamsByCategory:Array = new Array();
		streamArray.forEach( function(element:*, index:int, arr:Array):void {
			if((Stream(streamArray[index])).category.id == category)
				streamsByCategory.push(streamArray[index]);
		});
		mySphereContainer.updateSphere(streamsByCategory);
	} else {
		mySphereContainer.updateSphere(streamArray);
	}
}

private function search(searchTempl:String):void {
	
	var searchResultStreams:Array = new Array();
	streams.forEach( function(element:*, index:int, arr:Array):void {
		if((Stream(streams[index])).name.search(searchTempl) != -1)
			searchResultStreams.push(streams[index]);
	});
	filterByCategory(searchResultStreams);
}

public function onCreationComplete(event:FlexEvent):void {
	
	mySphereContainer.init();
	
	if (ExternalInterface.available) {
		ExternalInterface.addCallback("update", update);
		ExternalInterface.addCallback("search", search);
	}
	
	limeTalks = new LimeTalksSphere();
	limeTalks.addEventListener(LimeEvent.CONNECTION, onConnectionRise);
	limeTalks.addEventListener(LimeEvent.STREAM_LIST, onStreamListReceive);
	
	limeTalks.addEventListener(LimeEvent.STREAM_START, onStreamStart);
	limeTalks.addEventListener(LimeEvent.STREAM_STOP, onStreamStop);
	
	limeTalks.connect("","");		
}

private function onConnectionRise(event:LimeEvent):void {
	
	if(event.params.success)
		limeTalks.getStreams();		
	else
		log("Connection Failed");
}

private function onStreamListReceive(event:LimeEvent):void {
	
	streams = event.params.streams;
	mySphereContainer.updateSphere(streams);
}

private function onStreamStart(event:LimeEvent):void {
	  
	//log("Stream started: " + event.params.stream.id);
	var stream:Stream = Stream(event.params.stream);
	streams.push(stream);
	if(category == 0 || stream.category.id == category)
		filterByCategory(streams);
}

private function onStreamStop(event:LimeEvent):void {
	
	//log("Stream stopped: " + event.params.stream.id);
	var stream:Stream = Stream(event.params.stream);
	var indexToRemove:int = 0;
	streams.forEach( function(element:*, index:int, arr:Array):void {
		if((Stream(streams[index])).id == stream.id)
			indexToRemove = index;
	});
	streams.splice(indexToRemove,1);
	
	if(category == 0 || stream.category.id == category)
		filterByCategory(streams);
}

private function log(text:String):void {	
	Alert.show(text);
}
